package com.zz.hhbl.receiver

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/28 17:43.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import android.util.Log
import android.widget.Toast
import com.zz.framework.core.event.PowerConnectedEvent
import com.zz.framework.core.utils.DeviceUtils
import com.zz.framework.core.utils.LogUtils
import org.greenrobot.eventbus.EventBus

class ChargingReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            Intent.ACTION_POWER_CONNECTED -> {
                // 设备开始充电
                LogUtils.debug( "设备正在充电")
                EventBus.getDefault().post(PowerConnectedEvent(true))
            }
            Intent.ACTION_POWER_DISCONNECTED -> {
                // 设备停止充电
                LogUtils.debug( "设备已断开充电")
            }
            Intent.ACTION_BATTERY_CHANGED -> {
                // 监听电池状态
                val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
                val isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
                val chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
                val usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
                val acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC
                val chargingType = when (chargePlug) {
                    BatteryManager.BATTERY_PLUGGED_USB -> "USB 充电"
                    BatteryManager.BATTERY_PLUGGED_AC -> "AC 充电"
                    BatteryManager.BATTERY_PLUGGED_WIRELESS -> "无线充电"
                    else -> "未充电"
                }
                LogUtils.debug("充电状态: $isCharging, 类型: $chargingType")
                if (isCharging){
//                    EventBus.getDefault().post(PowerConnectedEvent(true))
                }
            }
        }
    }
}